import { CanvasWindy } from './water.js';
import axios from 'axios';
import Api from '@/api'
const Field = {
  canvasWindyIns: undefined,
  initWindyData() {
    //调整风场canvas大小
    const resizeCanvas = function (windy) {
      if (windycanvas == null) {
        return;
      }
      windycanvas.width = window.innerWidth * window.devicePixelRatio;
      windycanvas.height = window.innerHeight * window.devicePixelRatio;
      if (windy) {
        windy._resize(windycanvas.width, windycanvas.height);
      }
    };
    let waterArry = [];
    //获取风场数据、生成风场
    const getwindData = () => {
      Api.getFieldDataWithUVFilter()
        .then((res) => {
          //设置canvas
          var windycanvas = document.createElement('canvas');
          windycanvas.setAttribute("id", "windycanvas");
          windycanvas.style.position = 'fixed'
          windycanvas.style["pointer-events"] = "none";
          windycanvas.style["z-index"] = 0;
          windycanvas.style["bottom"] = 0;
          windycanvas.style["top"] = 0;
          windycanvas.style["left"] = 0;
          windycanvas.style["right"] = 0;
          document.getElementById('cesium-container').appendChild(windycanvas);

          resizeCanvas(windy);

          //风场参数设置
          const params = {
            viewer: viewer,
            canvas: windycanvas,
            canvasWidth: window.innerWidth * window.devicePixelRatio,
            canvasHeight: window.innerHeight * window.devicePixelRatio,
            speedRate: 5000,
            particlesNumber: 100000,
            maxAge: 20,
            frameRate: 60,
            color: '#ffff00',
            lineWidth: 1,
            waterPosition: waterArry
          };

          var windy;
          setTimeout(() => {
            Field.canvasWindyIns = new CanvasWindy(res, params);
          }, 0);
        })
    }

    Api.getFieldDataWithUV().then(res => {
      // 取前 67732 个点
      waterArry = res.features.slice(0, 67732);
      getwindData();
    })
  },
  removeWindy() {
    if (Field.canvasWindyIns) {
      Field.canvasWindyIns.removeLines();
    }
  }
}
export default Field;